﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZombieMonkeys.Vibria.Logic;
using System.Xml.Linq;
using System.IO.IsolatedStorage;
using System.IO;

namespace ZombieMonkeys.Vibria.DAL
{
    /// <summary>
    /// A class that will save out a current Sudoku Puzzle as an xml file
    /// </summary>
    public class PuzzleSaver
    {
        public void Save(Puzzle puzzle)
        {
            string path = Path.GetFullPath("SudokunatortronData.xml");
            string value = string.Empty;
            foreach (Square square in puzzle.Grid.SquareList)
            {
                if (square != puzzle.Grid.SquareList[puzzle.Grid.SquareList.Count - 1])
                {
                    value += square.Value.ToString() + ",";
                }
                else
                {
                    value += square.Value.ToString();
                }
            }
            XDocument doc = XDocument.Load(path);
            int i = 1;
            foreach (XElement elem in doc.Root.Elements("Puzzle"))
            {
                i++;
            }
            XElement element = new XElement(new XElement("Puzzle", new XAttribute("Id", i), new XElement("Grid", value)));
            doc.Root.Add(element);
            StreamWriter writer = new StreamWriter(path); 
            doc.Save(writer);
            writer.Close();
        }
    }
}
